//
//  TypeViewController.m
//  Journey
//
//  Created by dllo on 16/5/20.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "TypeViewController.h"
#import "SSNetworking.h"
#import "TypeTableViewCell.h"
#import "TypeCollectionViewCell.h"

#import "YNDataReading.h"
static NSString *const TypeViewCell = @"TypeViewCELL";
static NSString *const TypeCollectionCell = @"TypeCollectionCELL";
@interface TypeViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,strong)UITableView *typeTableView;
@property(nonatomic,strong)BaseCollectionView *typeCollectionView;
@property(nonatomic,strong)NSMutableArray *nameArray;
@property(nonatomic,strong)NSMutableArray *groupArray;

@end

@implementation TypeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
 //   self.view.backgroundColor = [UIColor whiteColor];
    self.nameArray = [NSMutableArray array];
    self.groupArray = [NSMutableArray array];
    
    //加载条
    //显示
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [self getData];
    
    self.typeTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width / 4, self.view.frame.size.height - 102) style:UITableViewStylePlain];
    [self.view addSubview:self.typeTableView];
    self.typeTableView.delegate = self;
    self.typeTableView.dataSource = self;
    //取消滚动条
    self.typeTableView.showsVerticalScrollIndicator = NO;
    //取消横线
    self.typeTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.typeTableView registerClass:[TypeTableViewCell class] forCellReuseIdentifier:TypeViewCell];
    self.typeTableView.dk_backgroundColorPicker = DKColorPickerWithRGB(0xFFFFFF,0x363636);
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(260 * WIDTHC, 100 * WIDTHC);
    flowLayout.minimumLineSpacing = 10;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.typeCollectionView = [[BaseCollectionView alloc] initWithFrame:CGRectMake(self.typeTableView.frame.size.width + 20, 0, self.view.frame.size.width - self.typeTableView.frame.size.width - 40, self.view.frame.size.height) collectionViewLayout:flowLayout];
   self.typeCollectionView.dk_backgroundColorPicker = DKColorPickerWithRGB(0xFFFFFF,0x363636);
    self.typeCollectionView.delegate = self;
    self.typeCollectionView.dataSource = self;
    self.typeCollectionView.showsVerticalScrollIndicator = NO;
    [self.typeCollectionView registerClass:[TypeCollectionViewCell class] forCellWithReuseIdentifier:TypeCollectionCell];
    [self.view addSubview:self.typeCollectionView];
    
    
    
    
}


-(void)getData{
    
    [SSNetworking ssGETWithURL:@"http://open.qyer.com/qyer/bbs/entry?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88283798007659&lon=121.539156053574&oauth_token=4ea07b0d41e13f5cb1448a45a3613e45&page=1&track_app_channel=App%2520Store&track_app_version=6.9.0&track_device_info=iPhone8%2C1&track_deviceid=F417850E-117E-4BF4-AAA6-3AD14E14E3C1&track_os=ios%25209.3.1&track_user_id=7891151&v=1" parameter:nil success:^(id result) {
        [[YNDataReading shareYNataReading]writeCachePlist:@"typeData" result:result];
        NSDictionary *dic = [[YNDataReading shareYNataReading]readCachePlist:@"typeData"];
        
        NSDictionary *dataDic = [dic objectForKey:@"data"];
        NSMutableArray *forum = [dataDic objectForKey:@"forum_list"];
        for (NSDictionary *nameDic in forum) {
            TypeModel *type = [[TypeModel alloc] init];
            [type setValuesForKeysWithDictionary:nameDic];
            [self.nameArray addObject:type];
        }
        
        
        [self.typeTableView reloadData];
        
        //默认点击第一个cell
        [self.typeTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
        
        //右边的collectionview也走第一个cell
        TypeModel *type = [self.nameArray objectAtIndex:0];
        
        [self.groupArray addObjectsFromArray:type.group];
        
        
        //隐藏
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        [self.typeCollectionView reloadData];
        
    } failure:^(NSError *error) {
        
        
    } network:^(BOOL networking) {
        NSDictionary *dic = [[YNDataReading shareYNataReading]readCachePlist:@"typeData"];
        NSDictionary *dataDic = [dic objectForKey:@"data"];
        NSMutableArray *forum = [dataDic objectForKey:@"forum_list"];
        for (NSDictionary *nameDic in forum) {
            TypeModel *type = [[TypeModel alloc] init];
            [type setValuesForKeysWithDictionary:nameDic];
            [self.nameArray addObject:type];
        }
        
        
        [self.typeTableView reloadData];
        
        //默认点击第一个cell
        [self.typeTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
        
        //右边的collectionview也走第一个cell
        TypeModel *type = [self.nameArray objectAtIndex:0];
        
        [self.groupArray addObjectsFromArray:type.group];
        
        
        //隐藏
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        [self.typeCollectionView reloadData];
        
    }];
    
    
    
    
 }





//collection
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.groupArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    TypeCollectionViewCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:TypeCollectionCell forIndexPath:indexPath];
    
    
    groupModel *group = [self.groupArray objectAtIndex:indexPath.row];
   
    item.group = group;
     return item;
}

//tableView
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.nameArray.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TypeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TypeViewCell];
  //  cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
  //  cell.selectedBackgroundView.backgroundColor = [UIColor redColor];
    
    TypeModel *type = [self.nameArray objectAtIndex:indexPath.row];
    
    cell.backgroundColor = YNColor(207, 207, 207);
    
    cell.typeModel = type;
   
    return cell;
}

//tableview点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TypeModel *type = [self.nameArray objectAtIndex:indexPath.row];
    
    [self.groupArray removeAllObjects];
    [self.groupArray addObjectsFromArray:type.group];
    
    [self.typeCollectionView reloadData];
     
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
